package com.ag.dao;

import java.util.List;
import java.util.Set;
import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import com.ag.bean.TbMedicineConfirm;

import com.ag.bean.TbQuantityUnit;

/**
 * 药品单位表的DAO
 * @author 李志伟
 *
 */

public class TbQuantityUnitDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(TbQuantityUnitDAO.class);
	// property constants
	public static final String QU_NAME = "quName";

	public void save(TbQuantityUnit transientInstance) {
		log.debug("saving TbQuantityUnit instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TbQuantityUnit persistentInstance) {
		log.debug("deleting TbQuantityUnit instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TbQuantityUnit findById(java.lang.String id) {
		log.debug("getting TbQuantityUnit instance with id: " + id);
		try {
			TbQuantityUnit instance = (TbQuantityUnit) getSession().get(
					"com.ag.bean.TbQuantityUnit", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<TbQuantityUnit> findByExample(TbQuantityUnit instance) {
		log.debug("finding TbQuantityUnit instance by example");
		try {
			List<TbQuantityUnit> results = (List<TbQuantityUnit>) getSession()
					.createCriteria("com.ag.bean.TbQuantityUnit").add(
							create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TbQuantityUnit instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from TbQuantityUnit as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<TbQuantityUnit> findByQuName(Object quName) {
		return findByProperty(QU_NAME, quName);
	}

	public List findAll() {
		log.debug("finding all TbQuantityUnit instances");
		try {
			String queryString = "from TbQuantityUnit";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TbQuantityUnit merge(TbQuantityUnit detachedInstance) {
		log.debug("merging TbQuantityUnit instance");
		try {
			TbQuantityUnit result = (TbQuantityUnit) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TbQuantityUnit instance) {
		log.debug("attaching dirty TbQuantityUnit instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TbQuantityUnit instance) {
		log.debug("attaching clean TbQuantityUnit instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}
